package org.idea.spring.datasource.dbcp;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * @Author linhao
 * @Date created in 12:20 下午 2021/5/17
 */
@Slf4j
@Configuration
public class DbcpDataSourceConfig {

    @Bean
    @ConditionalOnExpression("#{'dbcp'.equals(environment['qiyu.default.datasource.type'])}")
    public DbcpSimpleDataSourceProperties dbcpSimpleDataSourceProperties(){
        return new DbcpSimpleDataSourceProperties();
    }

    @Bean
    @ConditionalOnBean(value = DbcpSimpleDataSourceProperties.class)
    public BasicDataSource basicDataSource(DbcpSimpleDataSourceProperties dbcpSimpleDataSourceProperties){
        log.info("[DbcpDataSourceConfig] basicDataSource is : "+dbcpSimpleDataSourceProperties.toString());
        BasicDataSource basicDataSource = new BasicDataSource();
        basicDataSource.setUrl(dbcpSimpleDataSourceProperties.getUrl());
        basicDataSource.setDriverClassName(dbcpSimpleDataSourceProperties.getDriver());
        basicDataSource.setUsername(dbcpSimpleDataSourceProperties.getUsername());
        basicDataSource.setPassword(dbcpSimpleDataSourceProperties.getPassword());
        return basicDataSource;
    }
}
